﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ExameRegister.Views;
using System.Collections.ObjectModel;
using ExameRegister.Model;

namespace ExameRegister.Assert
{
    public partial class FlipSwitch : UserControl
    {
        private IAsyncCtrl _newPage;
        private UIElement _pageFrame;

        public FlipSwitch(UIElement ui)
        {
            InitializeComponent();
            LayoutRoot.Children.Remove(pageBorder);
            glassFrame.Content = pageBorder;
            ServiceUtility su = new ServiceUtility();
           
            su.CallService("ExameRegister.BO.LoginBO", "GetPublicOfficialInfo", new object[] { "Status = 1  order by inputtime desc" }, this);
            su.OnCompletedCallService += (sender, e1) =>
            {
                ObservableCollection<PublicOfficialInfo> collection = Utility.ConvertFromJson<ObservableCollection<PublicOfficialInfo>>(e1.Result);
                if (collection.Count > 0)
                {
                    InfomationControl control = new InfomationControl(collection[0]) { Margin=new Thickness(0,0,0,0)};
                    NavMoveSwitch Nav = new NavMoveSwitch();
                    control.Nav = Nav;
                    control.Nav.FirstPage = control;
                    this.SetNewPage(control, control.Nav);
                }
                else
                {
                    ErrorControl control = new ErrorControl(ui);
                    NavMoveSwitch Nav = new NavMoveSwitch();
                    control.Nav = Nav;
                    control.Nav.FirstPage = control;
                    this.SetNewPage(control, control.Nav);

                }
            };
           
        }

        public void SetNewPage(IAsyncCtrl newPage, UIElement pageFrame)
        {
            if (_newPage == null)
            {
                if (!Contains(pageFrame))
                {
                    pagesGrid.Children.Add(pageFrame);

                    newPage.PageLoaded = true;
                }
            }
            else
            {
                if (!Contains(pageFrame))
                {
                    pageFrame.Visibility = Visibility.Collapsed;
                    pagesGrid.Children.Add(pageFrame);
                    newPage.PageLoaded = false;
                }
                sbToBack.Begin();
            }

            _newPage = newPage;
            _pageFrame = pageFrame;
        }

        private void toBack_Completed(object sender, EventArgs e)
        {
            HidePages();
            _pageFrame.Visibility = Visibility.Visible;
            sbToFront.Begin();
        }

        private void toFront_Completed(object sender, EventArgs e)
        {
            if (!_newPage.PageLoaded)
            {
                _newPage.PageLoaded = true;
            }
        }

        private void HidePages()
        {
            foreach (UIElement i in pagesGrid.Children)
            {
                if (i.Visibility == Visibility.Visible)
                {
                    i.Visibility = Visibility.Collapsed;
                }
            }
        }

        private bool Contains(UIElement el)
        {
            foreach (UIElement i in pagesGrid.Children)
            {
                if (i == el)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
